﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using zichan.BLL.StatisticsModel;
using zichan.DAL;
using zichan.Entity;
namespace zichan.BLL
{
    public class APIService
    {
        public static IpAPIModel GetIps(string ip)
        {
            using (ZichanDBContext db = new ZichanDBContext())
            {
                IpAPIModel model = new IpAPIModel();
                model.IsDJ = true;
                model.Ip = ip;
                int? purposeId = null;
                IP ipModel = db.IPs.FirstOrDefault(o => o.Ip.Equals(ip));
                if (ipModel == null)
                {
                    model.IsDJ = false;
                    return model; }
                string id = ipModel.Id.ToString();
                if (db.PutawayServers.FirstOrDefault(o => o.Statu == 1 && (o.IPId == ipModel.Id || (!string.IsNullOrEmpty(o.SubIPIds) && o.SubIPIds.Contains(id)))) != null)
                {
                    purposeId = db.PutawayServers.FirstOrDefault(o => o.Statu == 1 && (o.IPId == ipModel.Id || (!string.IsNullOrEmpty(o.SubIPIds) && o.SubIPIds.Contains(id)))).serverPurposeSubId;
                }
                int mClassId= db.MachineRooms.FirstOrDefault(o => o.Id.Equals(ipModel.MachineRoomId)).MachineRoomClassId;
                model.IpAdress= db.MachineRoomClasses.FirstOrDefault(o => o.Id.Equals(mClassId)).Name;
                //model.IpAdress = db.MachineRooms.FirstOrDefault(o => o.Id.Equals(ipModel.MachineRoomId)).Name;
                if (purposeId.HasValue)
                    model.IpClass = db.ServerPurposeSubClasses.FirstOrDefault(o => o.serverPurposeSubId== purposeId).serverPurposeSubName;
               
                return model;
            }
        }
    }
}
